summaryrefslogtreecommitdiffstats
path: root/src/core/file_sys/fs_save_data_types.h
blob: 02bbc329429f68606c5dd065da1658e5e1f6b1a2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// SPDX-FileCopyrightText: Copyright 2024 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <array>
#include "common/common_funcs.h"
#include "common/common_types.h"

namespace FileSys {

using SaveDataId = u64;
using SystemSaveDataId = u64;
using SystemBcatSaveDataId = SystemSaveDataId;
using ProgramId = u64;

enum class SaveDataSpaceId : u8 {
    System = 0,
    User = 1,
    SdSystem = 2,
    Temporary = 3,
    SdUser = 4,

    ProperSystem = 100,
    SafeMode = 101,
};

enum class SaveDataType : u8 {
    System = 0,
    Account = 1,
    Bcat = 2,
    Device = 3,
    Temporary = 4,
    Cache = 5,
    SystemBcat = 6,
};

enum class SaveDataRank : u8 {
    Primary = 0,
    Secondary = 1,
};

struct SaveDataSize {
    u64 normal;
    u64 journal;
};
static_assert(sizeof(SaveDataSize) == 0x10, "SaveDataSize has invalid size.");

using UserId = u128;
static_assert(std::is_trivially_copyable_v<UserId>, "Data type must be trivially copyable.");
static_assert(sizeof(UserId) == 0x10, "UserId has invalid size.");

constexpr inline SystemSaveDataId InvalidSystemSaveDataId = 0;
constexpr inline UserId InvalidUserId = {};

enum class SaveDataFlags : u32 {
    None = (0 << 0),
    KeepAfterResettingSystemSaveData = (1 << 0),
    KeepAfterRefurbishment = (1 << 1),
    KeepAfterResettingSystemSaveDataWithoutUserSaveData = (1 << 2),
    NeedsSecureDelete = (1 << 3),
};

enum class SaveDataMetaType : u8 {
    None = 0,
    Thumbnail = 1,
    ExtensionContext = 2,
};

struct SaveDataMetaInfo {
    u32 size;
    SaveDataMetaType type;
    INSERT_PADDING_BYTES(0xB);
};
static_assert(std::is_trivially_copyable_v<SaveDataMetaInfo>,
              "Data type must be trivially copyable.");
static_assert(sizeof(SaveDataMetaInfo) == 0x10, "SaveDataMetaInfo has invalid size.");

struct SaveDataCreationInfo {
    s64 size;
    s64 journal_size;
    s64 block_size;
    u64 owner_id;
    u32 flags;
    SaveDataSpaceId space_id;
    bool pseudo;
    INSERT_PADDING_BYTES(0x1A);
};
static_assert(std::is_trivially_copyable_v<SaveDataCreationInfo>,
              "Data type must be trivially copyable.");
static_assert(sizeof(SaveDataCreationInfo) == 0x40, "SaveDataCreationInfo has invalid size.");

struct SaveDataAttribute {
    ProgramId program_id;
    UserId user_id;
    SystemSaveDataId system_save_data_id;
    SaveDataType type;
    SaveDataRank rank;
    u16 index;
    INSERT_PADDING_BYTES(0x1C);

    static constexpr SaveDataAttribute Make(ProgramId program_id, SaveDataType type, UserId user_id,
                                            SystemSaveDataId system_save_data_id, u16 index,
                                            SaveDataRank rank) {
        return {
            .program_id = program_id,
            .user_id = user_id,
            .system_save_data_id = system_save_data_id,
            .type = type,
            .rank = rank,
            .index = index,
        };
    }

    static constexpr SaveDataAttribute Make(ProgramId program_id, SaveDataType type, UserId user_id,
                                            SystemSaveDataId system_save_data_id, u16 index) {
        return Make(program_id, type, user_id, system_save_data_id, index, SaveDataRank::Primary);
    }

    static constexpr SaveDataAttribute Make(ProgramId program_id, SaveDataType type, UserId user_id,
                                            SystemSaveDataId system_save_data_id) {
        return Make(program_id, type, user_id, system_save_data_id, 0, SaveDataRank::Primary);
    }

    std::string DebugInfo() const {
        return fmt::format(
            "[title_id={:016X}, user_id={:016X}{:016X}, save_id={:016X}, type={:02X}, "
            "rank={}, index={}]",
            program_id, user_id[1], user_id[0], system_save_data_id, static_cast<u8>(type),
            static_cast<u8>(rank), index);
    }
};
static_assert(sizeof(SaveDataAttribute) == 0x40);
static_assert(std::is_trivially_destructible<SaveDataAttribute>::value);

constexpr inline bool operator<(const SaveDataAttribute& lhs, const SaveDataAttribute& rhs) {
    return std::tie(lhs.program_id, lhs.user_id, lhs.system_save_data_id, lhs.index, lhs.rank) <
           std::tie(rhs.program_id, rhs.user_id, rhs.system_save_data_id, rhs.index, rhs.rank);
}

constexpr inline bool operator==(const SaveDataAttribute& lhs, const SaveDataAttribute& rhs) {
    return std::tie(lhs.program_id, lhs.user_id, lhs.system_save_data_id, lhs.type, lhs.rank,
                    lhs.index) == std::tie(rhs.program_id, rhs.user_id, rhs.system_save_data_id,
                                           rhs.type, rhs.rank, rhs.index);
}

constexpr inline bool operator!=(const SaveDataAttribute& lhs, const SaveDataAttribute& rhs) {
    return !(lhs == rhs);
}

struct SaveDataExtraData {
    SaveDataAttribute attr;
    u64 owner_id;
    s64 timestamp;
    u32 flags;
    INSERT_PADDING_BYTES(4);
    s64 available_size;
    s64 journal_size;
    s64 commit_id;
    INSERT_PADDING_BYTES(0x190);
};
static_assert(sizeof(SaveDataExtraData) == 0x200, "SaveDataExtraData has invalid size.");
static_assert(std::is_trivially_copyable_v<SaveDataExtraData>,
              "Data type must be trivially copyable.");

struct HashSalt {
    static constexpr size_t Size = 32;

    std::array<u8, Size> value;
};
static_assert(std::is_trivially_copyable_v<HashSalt>, "Data type must be trivially copyable.");
static_assert(sizeof(HashSalt) == HashSalt::Size);

} // namespace FileSys